#pragma once

#include "const.h"
#include "Singleton.h"
#include "ConfigManager.h"
#include "message.grpc.pb.h"

#include <grpc++/grpc++.h>

class StatusConnectPool : public Singleton<StatusConnectPool>
{
	friend class Singleton<StatusConnectPool>;
	friend class StatusGrpcClient;

public:
	~StatusConnectPool();

	std::unique_ptr<message::StatusService::Stub> getConnection();
	void returnConnection(std::unique_ptr<message::StatusService::Stub>  connection);
	void Close();

private:
	StatusConnectPool(std::string host, std::string port, size_t pool_size);

private:
	std::string m_host;
	std::string m_port;
	size_t m_pool_size;
	std::atomic_bool m_b_stop;
	std::mutex m_mutex;
	std::condition_variable m_cond;
	std::queue<std::unique_ptr<message::StatusService::Stub>> m_connections;
};

class StatusGrpcClient : public Singleton<StatusGrpcClient>
{
	friend class Singleton<StatusGrpcClient>;

public:
	~StatusGrpcClient() = default;
	// 从状态服务器中获取聊天服务器信息（IP和token）
	message::GetChatServerRsp getChatServer(int uid);
	// TODO: 增加其他接口

private:
	StatusGrpcClient();

private:
	std::unique_ptr<StatusConnectPool> m_pool;
};

